<template>
  <div class="icons-wrapper">
    <swiper  :options="swiperOption">
      <swiper-slide v-for="(page, idx) of pages" :key="idx">
        <div class="icon-box" 
             v-for="item of page"
             :key="item.id"
        >
          <div class="icon-img-box">
            <img class="icon-img" :src="item.imgUrl" alt="" />
          </div>
          <p class="icon-description">{{ item.desc }}</p>
        </div>   
      </swiper-slide>
    </swiper>
  </div>
</template>

<script>
var iconsBaseUrl = 'http://fep.lovemydearly.com/07Vue/img/home-icons/'
export default {
  name: 'HomeIcons',
  props: {
    iconsList: [Array]
  },
  data: function() {
    return {
      swiperOption: {
        // 自动播放默认为false, 所以只要放个对象就行
        // loop : true, //是否循环(拉到最后或最前继续拉会循环)
      },
    }
  },
  computed: {
    pages: function() {
      var pages = [];
      this.iconsList.forEach(function(item, index) {
        var page = Math.floor(index / 8);
        if (!pages[page]) {
          pages[page] = [];
        }
        pages[page].push(item);
      });

      return pages;
    }
  }
}
</script>

<style lang="stylus" scoped>
  @import '~@styles/variables.styl';
  @import '~@styles/mixins.styl';

  .icons-wrapper >>> .swiper-container {
    overflow: hidden;
    width: 100%;
    height: 0;
    // padding为%时, 它的length和父级的"宽度"有关(mdn里有的)
    // 可以在不同设备上等比例
    padding-bottom: 50%;
    // background-color: orange;
  }

  // 这个地方由.swiper-container撑开, 所以不用写样式
  .icons-wrapper {
    margin: .2rem 0;
  }

  .icons-wrapper .icon-box {
    position: relative;
    float: left;
    width: 25%;
    height: 0;
    padding-bottom: 25%;
    // background-color: green;
  }

  .icons-wrapper .icon-box .icon-img-box {
    position: absolute;
    top: 0;
    bottom: .44rem;
    left: 0;
    // absolute就算是块级也要自己设置width
    width: 100%;
    // background-color: lightpink;
  }

  .icons-wrapper .icon-box .icon-img-box .icon-img {
    display: block;
  }

  .icons-wrapper .icon-box .icon-description {
    position: absolute;
    bottom: 0;
    left: 0;
    width: 100%;
    height: .44rem;
    line-height: .44rem;
    text-align: center;
    color: $darkTxtColor;
    ellipsis();
  }
</style>